package com.fyx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fyx.entity.StayUserEntity;
import com.fyx.entity.SysUser;
import com.fyx.mapper.StayUserMapper;
import com.fyx.mapper.SysUserMapper;
import com.fyx.service.IStayUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StayUserServiceImpl extends ServiceImpl<StayUserMapper, StayUserEntity> implements IStayUserService {

    @Autowired
    private StayUserMapper stayUserMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public Map getStayUserList(StayUserEntity stayUserEntity, Pageable pageable){
        Page<StayUserEntity> page = new Page<>(pageable.getPageNumber()+1,pageable.getPageSize());
        QueryWrapper<StayUserEntity> wrapper =new QueryWrapper<>();
        if(!CollectionUtils.isEmpty(stayUserEntity.getDeptIds())){
            List<Long> deptIds= new ArrayList<>(stayUserEntity.getDeptIds());
            List<SysUser> channels = sysUserMapper.getUserNames(deptIds);
            String channel[] = new String[channels.size()];
            for(int i=0; i<channels.size();i++){
                channel[i]=channels.get(i).getUsername();
                System.out.println("channel:=========="+channels.get(i).getUsername());
            }
            wrapper.in("channel",channel);
          //  wrapper.eq("the_father","total");
        }else {
            if(stayUserEntity.getChannel()!=null){
                wrapper.eq("channel",stayUserEntity.getChannel());
            }else{
                wrapper.eq("channel","total").or().isNull("channel");
            }
            if(stayUserEntity.getTime()!=null&&stayUserEntity.getTime()>0){
                wrapper.eq("time",stayUserEntity.getTime());
                wrapper.ne("channel","total");
            }
        }

        if(stayUserEntity.getQTime()!=null){
            wrapper.ge("time",stayUserEntity.getQTime().get(0).getTime()/1000);
            wrapper.le("time",stayUserEntity.getQTime().get(1).getTime()/1000);
        }

        wrapper.orderBy(true,false,"time");

        IPage<StayUserEntity> ipage = stayUserMapper.selectPage(page,wrapper);
        List<StayUserEntity> list = ipage.getRecords();
        long total = ipage.getTotal();
        Map map =new HashMap();
        map.put("list",list);
        map.put("total",total);
        return map;
    }

    @Override
    public List<StayUserEntity> getStayUserList(Integer time,String channel){
        QueryWrapper<StayUserEntity> wrapper =new QueryWrapper<>();
        if(time!=null&&time>0){
            wrapper.eq("time",time);
            wrapper.eq("the_father",channel);
        }
        wrapper.orderBy(true,false,"time");
        List<StayUserEntity> list = stayUserMapper.selectList(wrapper);
        if(list.size()>0){
            for(StayUserEntity stayUserEntity:list){
                if(stayUserEntity.getChannel().equals("h5hzmjxl_wechat")||stayUserEntity.getChannel().equals("h5hzmjxl_qq")||stayUserEntity.getChannel().equals("h5hcmjxl_wechat")||stayUserEntity.getChannel().equals("h5gdmjxl_wechat")) {
                    if(stayUserEntity.getTheFather().equals("h5hzmjxl_wechat")||stayUserEntity.getTheFather().equals("h5hzmjxl_qq")||stayUserEntity.getTheFather().equals("h5hcmjxl_wechat")||stayUserEntity.getTheFather().equals("h5gdmjxl_wechat")){
                        stayUserEntity.setHasChildren(false);
                    }
                }else if(stayUserEntity.getChannel().indexOf("total")>0){
                    stayUserEntity.setHasChildren(true);
                }else {
                    stayUserEntity.setHasChildren(false);
                }
            }
        }
        return list;
    }
}
